<html>
<head><meta charset="utf-8"><title>Behavior of Drop and variable overrides · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Behavior.20of.20Drop.20and.20variable.20overrides.html">Behavior of Drop and variable overrides</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="226077253"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Behavior%20of%20Drop%20and%20variable%20overrides/near/226077253" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jeremiah Senkpiel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Behavior.20of.20Drop.20and.20variable.20overrides.html#226077253">(Feb 12 2021 at 00:09)</a>:</h4>
<p>If you do something like this, is the original dropped on <code>clone</code>, or at the end of the function?</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">thing</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">std</span>::<span class="n">env</span><span class="p">(</span><span class="s">"ENV"</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="c1">// ...</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">thing</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">thing</span><span class="p">.</span><span class="n">clone</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="c1">// ...</span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="226077364"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Behavior%20of%20Drop%20and%20variable%20overrides/near/226077364" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Behavior.20of.20Drop.20and.20variable.20overrides.html#226077364">(Feb 12 2021 at 00:10)</a>:</h4>
<p>end of the function</p>



<a name="226077394"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Behavior%20of%20Drop%20and%20variable%20overrides/near/226077394" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jeremiah Senkpiel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Behavior.20of.20Drop.20and.20variable.20overrides.html#226077394">(Feb 12 2021 at 00:10)</a>:</h4>
<p>Good to know.</p>



<a name="226077464"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Behavior%20of%20Drop%20and%20variable%20overrides/near/226077464" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Behavior.20of.20Drop.20and.20variable.20overrides.html#226077464">(Feb 12 2021 at 00:11)</a>:</h4>
<p>If you want to drop it explicitly, you should put it in a scope like</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">thing</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">  </span><span class="kd">let</span><span class="w"> </span><span class="n">thing</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">std</span>::<span class="n">env</span><span class="p">(</span><span class="s">"ENV"</span><span class="p">);</span><span class="w"></span>
<span class="w">  </span><span class="n">thing</span><span class="p">.</span><span class="n">clone</span><span class="p">()</span><span class="w"></span>
<span class="p">};</span><span class="w"></span>
</code></pre></div>



<a name="226077491"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Behavior%20of%20Drop%20and%20variable%20overrides/near/226077491" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Behavior.20of.20Drop.20and.20variable.20overrides.html#226077491">(Feb 12 2021 at 00:11)</a>:</h4>
<p>or use <code>drop(thing)</code></p>



<a name="226077629"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Behavior%20of%20Drop%20and%20variable%20overrides/near/226077629" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jeremiah Senkpiel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Behavior.20of.20Drop.20and.20variable.20overrides.html#226077629">(Feb 12 2021 at 00:13)</a>:</h4>
<p>Does this also work with <code>if</code> blocks? I.e. can I move the clone (from line 116) to this line? <a href="https://github.com/http-rs/http-client/pull/59/files#diff-93473a521a8bd690c25ad5d390cc5d3f0c01542de868f3a11089faa596e80fe9R106">https://github.com/http-rs/http-client/pull/59/files#diff-93473a521a8bd690c25ad5d390cc5d3f0c01542de868f3a11089faa596e80fe9R106</a></p>



<a name="226077735"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Behavior%20of%20Drop%20and%20variable%20overrides/near/226077735" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jeremiah Senkpiel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Behavior.20of.20Drop.20and.20variable.20overrides.html#226077735">(Feb 12 2021 at 00:14)</a>:</h4>
<p>I suppose <code>std::mem::drop()</code> might be more explicit to future contributors.</p>



<a name="226077808"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Behavior%20of%20Drop%20and%20variable%20overrides/near/226077808" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Behavior.20of.20Drop.20and.20variable.20overrides.html#226077808">(Feb 12 2021 at 00:15)</a>:</h4>
<p>Yes, if you use <code>.clone()</code> in the if statement, then the borrow in <code>self.http_pools.get(&amp;addr)</code> will only scope until the end of the let statement (including the if but not past it)</p>



<a name="226077909"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Behavior%20of%20Drop%20and%20variable%20overrides/near/226077909" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Behavior.20of.20Drop.20and.20variable.20overrides.html#226077909">(Feb 12 2021 at 00:16)</a>:</h4>
<p>An easy way to try these things out is to see just how long will rustc allow you to keep a borrow of the original value for.</p>



<a name="226077959"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Behavior%20of%20Drop%20and%20variable%20overrides/near/226077959" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Behavior.20of.20Drop.20and.20variable.20overrides.html#226077959">(Feb 12 2021 at 00:17)</a>:</h4>
<p>Actually, looking at the code in the link, it seems like the first <code>pool</code> is a reference, which means that the drop will do nothing</p>



<a name="226077969"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Behavior%20of%20Drop%20and%20variable%20overrides/near/226077969" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Behavior.20of.20Drop.20and.20variable.20overrides.html#226077969">(Feb 12 2021 at 00:17)</a>:</h4>
<p>e.g. </p>
<div class="codehilite"><pre><span></span><code>let borrow = if true {
    let apple = ..;
    &amp;apple
} else {
    ...
}
drop(borrow);
</code></pre></div>



<a name="226077976"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Behavior%20of%20Drop%20and%20variable%20overrides/near/226077976" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jeremiah Senkpiel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Behavior.20of.20Drop.20and.20variable.20overrides.html#226077976">(Feb 12 2021 at 00:17)</a>:</h4>
<p>That doesn't help here, because I am trying to prevent thread deadlocks when using dashmap. I'm not having lifetime issues.</p>



<a name="226078027"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Behavior%20of%20Drop%20and%20variable%20overrides/near/226078027" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jeremiah Senkpiel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Behavior.20of.20Drop.20and.20variable.20overrides.html#226078027">(Feb 12 2021 at 00:18)</a>:</h4>
<p>Oh maybe I see what you are saying for trying it.</p>



<a name="226078063"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Behavior%20of%20Drop%20and%20variable%20overrides/near/226078063" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Behavior.20of.20Drop.20and.20variable.20overrides.html#226078063">(Feb 12 2021 at 00:18)</a>:</h4>
<p>yeah, I mean in terms of figuring out how long does the underlying value live for or when its dropped.</p>



<a name="226078126"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Behavior%20of%20Drop%20and%20variable%20overrides/near/226078126" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Behavior.20of.20Drop.20and.20variable.20overrides.html#226078126">(Feb 12 2021 at 00:19)</a>:</h4>
<p>if you're finding the lifetimes of values (not references) to be tricky in some situation and need a thing to be dropped early <code>drop(x)</code> is your and the person's after you best friend.</p>



<a name="226078148"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Behavior%20of%20Drop%20and%20variable%20overrides/near/226078148" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Behavior.20of.20Drop.20and.20variable.20overrides.html#226078148">(Feb 12 2021 at 00:19)</a>:</h4>
<p>I think it is good practice to always either scope or <code>drop(g)</code> any locks because otherwise they can end up in surprising places</p>



<a name="226078268"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Behavior%20of%20Drop%20and%20variable%20overrides/near/226078268" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jeremiah Senkpiel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Behavior.20of.20Drop.20and.20variable.20overrides.html#226078268">(Feb 12 2021 at 00:20)</a>:</h4>
<p><span class="user-mention" data-user-id="271719">@Mario Carneiro</span> The reference is a dashmap smart <code>Ref</code>: it impls <code>Deref</code>, not <code>Clone</code>: <a href="https://docs.rs/dashmap/4.0.2/dashmap/mapref/one/struct.Ref.html">https://docs.rs/dashmap/4.0.2/dashmap/mapref/one/struct.Ref.html</a></p>



<a name="226078286"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Behavior%20of%20Drop%20and%20variable%20overrides/near/226078286" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jeremiah Senkpiel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Behavior.20of.20Drop.20and.20variable.20overrides.html#226078286">(Feb 12 2021 at 00:20)</a>:</h4>
<p>(I.e. it's not a <code>RefCell</code> <code>Ref</code>)</p>



<a name="226078311"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Behavior%20of%20Drop%20and%20variable%20overrides/near/226078311" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jeremiah Senkpiel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Behavior.20of.20Drop.20and.20variable.20overrides.html#226078311">(Feb 12 2021 at 00:21)</a>:</h4>
<p>(Essentially a <code>RwLockReadGuard</code>)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>